package com.sh.data.engine.domain.dataasset.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.sh.data.engine.domain.base.model.enums.Database;
import com.sh.data.engine.domain.dataasset.model.domain.LineageDomain;
import com.sh.data.engine.domain.dataasset.model.domain.LineageMapDomain;
import com.sh.data.engine.domain.dataasset.model.domain.TableLineageDomain;
import com.sh.data.engine.domain.integration.offline.model.domain.FieldInfo;
import com.sh.data.engine.repository.dao.dataasset.entity.LineageEntity;
import com.sh.data.engine.repository.dao.datadev.entity.DatadevJobEntity;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: zigui.zdf @Date: 2022/3/14 16:44:22 @Description:
 */
public interface LineageService extends IService<LineageEntity> {

    /**
     * 分析sql的血缘关系
     *
     * @param sql
     * @param database
     * @param projectId
     * @return
     */
    List<LineageDomain> analyseSqlLineage(String sql, Database database, Long projectId);

    /**
     * 分析sql的血缘关系并插入到血缘表中
     *
     * @param sql
     * @param database
     * @param projectId
     * @param jobEntity
     */
    void analyseAndInsertSqlLineage(
        String sql, Database database, Long projectId, DatadevJobEntity jobEntity);

    /**
     * 分析sql对已经存在的表读写权限 写：update ,delete ,drop 读：select
     */
    String analyCreatetable(String sql, Database database);

    void analySapLineageAndInsert(String sql, Long projectId, DatadevJobEntity jobEntity);

    Map<String, Set<Long>> analyseSqlUsedAuthority(String sql, Database database, Long projectId);

    LineageMapDomain getLineage(Long tblId, Long projectId);

    String checkIsSelectSql(String sql, Database database);

    List<FieldInfo> getFieldInfoBySQLScrip(String sql);

    /**
     * 判断SQL是否对主数据数据进行更改或者表结构更改
     */
    boolean checkSQLUpdateMd(String sql, Database database);

    TableLineageDomain getLineageDetail(Long tblId, Long projectId);

    String generateSqlflowGraph(
        @RequestParam(required = true) String sql,
        @RequestParam(required = false) String database,
        @RequestParam(required = false) String schema, @RequestParam(required = false) String table,
        @RequestParam(required = false) String column,
        @RequestParam(required = false, defaultValue = "true") boolean isReturnModel,
        @RequestParam(required = true) String dbvendor,
        @RequestParam(required = false, defaultValue = "fdd") String showRelationType,
        @RequestParam(required = false, defaultValue = "false") boolean simpleOutput,
        @RequestParam(required = false, defaultValue = "false") boolean ignoreRecordSet,
        @RequestParam(required = false, defaultValue = "false") boolean treatArgumentsInCountFunctionAsDirectDataflow,
        @RequestParam(required = false, defaultValue = "false") boolean showConstantTable,
        @RequestParam(required = false, defaultValue = "false") boolean showTransform,
        @RequestParam(required = false, defaultValue = "true") boolean showLinkOnly,
        @RequestParam(required = false, defaultValue = "false") boolean hideColumn,
        @RequestParam(required = false, defaultValue = "false") boolean ignoreFunction,
        @RequestParam(required = false, defaultValue = "true") boolean normalizeIdentifier,
        @RequestParam(required = false) String jobId,
        @RequestParam(required = false) String columnLevel,
        @RequestParam(required = false, defaultValue = "false") boolean parallel,
        @RequestParam(required = true) String sqlHash,
        @RequestParam(required = false, defaultValue = "false") boolean containGraphML,
        @RequestParam(required = false, defaultValue = "false") boolean leftMostTable,
        @RequestParam(required = false, defaultValue = "false") boolean getFileContent,
        @RequestParam(required = false, defaultValue = "false") boolean forceTableLevel,
        @RequestParam(required = false) String defaultServer,
        @RequestParam(required = false) String defaultDatabase,
        @RequestParam(required = false) String defaultSchema);
}
